import 'package:aichat_flutter_wallet/kai_print.dart';
import 'package:aichat_flutter_wallet/module/module_trade/widgets/gasslow_tip.dart';
import 'package:aichat_flutter_wallet/module/module_trade/widgets/triangle_painter.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lib_database/lib_database.dart';
import 'package:lib_util/utils/utils.dart';

class GasList extends StatefulWidget {
  Token? token;
  Token? main;
  int gas;
  List<int> gasPriceList;
  Function call;
  GasList(this.token, this.main, this.gas, this.gasPriceList, this.call);
  @override
  _GasList createState() => _GasList();
}

class _GasList extends State<GasList> {
  int _gasType = 1;
  Widget _GasItem(int type) {
    List<String> gasTitleList = ["Fast", "Avg", "Slow"];
    List<String> gasTimeList = ["Around 20s", "Around 40s", "Around 50min"];
    List<Color> gasColorList = [
      Color.fromRGBO(58, 177, 151, 1),
      Color.fromRGBO(252, 168, 25, 1),
      Color.fromRGBO(255, 81, 0, 1)
    ];

    return ClipRect(
      child: GestureDetector(
        onTap: () {
          if (type == 2 && _gasType != 2) {
            GasslowTipDialog.showAboutGas(context);
          }
          if (type != _gasType) {
            widget.call(type);
            setState(() {
              _gasType = type;
            });
          }
        },
        child: Stack(
          children: [
            Container(
              width: double.infinity,
              padding: EdgeInsets.fromLTRB(5.w, 11.w, 5.w, 11.w),
              decoration: BoxDecoration(
                  color: _gasType == type ? Color.fromRGBO(134, 103, 254, 0.05) : Color.fromRGBO(246, 246, 246, 1),
                  border: _gasType == type ? Border.all(color: Color.fromRGBO(134, 103, 254, 1), width: 1.w) : null,
                  borderRadius: BorderRadius.all(Radius.circular(10.w))),
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      gasTitleList[type],
                      style:
                          TextStyle(fontSize: 12.sp, color: Color.fromRGBO(51, 51, 51, 1), fontWeight: FontWeight.w400),
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Text(
                      '${_calGasFee(type)} ${widget.token?.getChain()!.symbol}',
                      textAlign: TextAlign.center,
                      style: TextStyle(
                          fontSize: 10.sp,
                          height: 14.w / 10.w,
                          color: Color.fromRGBO(134, 131, 145, 1),
                          fontWeight: FontWeight.w400),
                    ),
                    SizedBox(
                      height: 2.w,
                    ),
                    Text(
                      '≈ \$${Utils.formatStringBalance(
                        (double.parse(_calGasFee(type)) *
                                double.parse((widget.main!.priceusd == null || widget.main!.priceusd == '')
                                    ? '0'
                                    : widget.main!.priceusd!))
                            .toString(),
                        fractionDigits: Config.priceNum,
                      )}',
                      style: TextStyle(
                          fontSize: 10.sp,
                          height: 14.w / 10.w,
                          color: Color.fromRGBO(134, 131, 145, 1),
                          fontWeight: FontWeight.w400),
                    ),
                    SizedBox(
                      height: 10.w,
                    ),
                    Text(
                      gasTimeList[type],
                      style: TextStyle(
                          fontSize: 10.sp, height: 14.w / 10.w, color: gasColorList[type], fontWeight: FontWeight.w400),
                    ),
                  ]),
            ),
            if (_gasType == type)
              Positioned(
                bottom: 1.w,
                right: 1.w,
                child: CustomPaint(
                  painter: TrianglePainter(),
                  child: Container(
                    width: 20.w,
                    height: 20.w,
                    // color: Color.fromRGBO(134, 103, 254, 1),
                    alignment: Alignment.bottomRight,
                    child: Image.asset(
                      'assets/images/wallet-trade/selected.png', // 替换为你的图片路径
                      width: 14.w,
                      height: 14.w,
                    ),
                  ),
                ),
              )
          ],
        ),
      ),
    );
  }

  String _calGasFee(int type) {
    Chain? chain = widget.token?.getChain();

    try{
      if (chain!.type == ChainType.eth) {
        if (0 == widget.gas || 0 == widget.gasPriceList[type]) {
          return '0.00';
        }
        return '${Utils.formatStringBalance((BigInt.parse(widget.gas.toString()) * BigInt.parse(widget.gasPriceList[type].toString()) / BigInt.from(10).pow(widget.main!.decimals)).toString(), fractionDigits: widget.main!.decimals, trimDecimalZero: true)}';
      } else {
        return '${Utils.formatStringBalance((BigInt.parse(widget.gas.toString()) * BigInt.parse(widget.gasPriceList[type].toString()) / BigInt.from(10).pow(widget.main!.decimals)).toString(), fractionDigits: widget.main!.decimals, trimDecimalZero: true)}';
      }
    }
    catch(e){
      kaiPrint('_calGasFee $e');
      return '0.00';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Expanded(child: _GasItem(0)),
        SizedBox(
          width: 7.w,
        ),
        Expanded(child: _GasItem(1)),
        SizedBox(
          width: 7.w,
        ),
        Expanded(child: _GasItem(2)),
      ],
    );
  }
}
